<?php

namespace app\api\controller\worker;


use app\admin\model\Roomclean;
use app\admin\model\Shopcharge;
use app\admin\model\Shoporder;
use app\admin\model\Shopstaff;
use app\admin\model\Shopwifi;
use app\admin\model\Userorderprofit;
use app\common\controller\Api;

/**
 *
 */
class Index extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function index(){

        $user = $this->auth->getUser();
        $shop_id = $this->request->param('shop_id');

        $starTime = strtotime("-720 minute",time());




        $orderModel  = new Shoporder();
        $roomCleanModel = new Roomclean();
        $list = $roomCleanModel->where('shop_id',$shop_id)->where('status','in',['done','doing','undo'])->where('end_time',">=",$starTime)->order('start_time','asc')->group('room_id')->select();

        $newList = array();
        foreach ($list as $key=>$val){
            $obj = array();

            $cleans = $roomCleanModel->with(['room','shop'])->where('Roomclean.status','in',['undo','doing'])->where('Roomclean.room_id',$val['room_id'])->where('end_time',">=",$starTime)->order('end_time','asc')->limit(4)->select();

            $newCleanList = array();
            foreach ($cleans as $key=>$val){
                $order = $orderModel->where('id',$val['order_id'])->find();
                if(!empty($order)){
                    $order['book_start_txt'] = date("m-d H:i",$order['book_start']);
                    $order['book_end_txt'] = date("m-d H:i",$order['book_end']);

                    if($order['book_end'] >time()){
                        $newCleanList[] = $order;
                    }
                    $cleans[$key]['order'] = $order;
                    
                }

            }
            $wh = array();
            $wh['Roomclean.room_id'] = $val['room_id'];




            $clean = $roomCleanModel->with(['room','shop'])->where($wh)->where("Roomclean.status",'in',['undo','doing'])->where('end_time',">=",$starTime)->where('start_time',"<",time())->find();
            if($clean['shop_id'] == 138){
                error_log("===clean=1=".json_encode($clean));
            }

            $obj['isShow'] = 1;
            if($clean == null){
                $clean = $roomCleanModel->with(['room','shop'])->where('Roomclean.room_id',$val['room_id'])->where("Roomclean.status",'in',['undo','doing'])->where('start_time',">=",time())->find();
                $obj['isShow'] = -1;
            }


            $obj['list'] = $cleans;
            $obj['list2'] = $newCleanList;

            $obj['logo'] = cdnurl($val['room']['logo'],true);
            $obj['room_name'] = $clean['room']['name'];
            $obj['shop_name'] = $clean['shop']['name'];
            $obj['id'] = $clean['id'];
            $obj['room_id'] = $clean['room_id'];


            if($clean['shop_id'] == 138){
                error_log("===clean=111=".json_encode($clean));
            }
            $obj['status_txt'] = $roomCleanModel->getStatus($val['status']);
            if(time() < $clean['start_time']){
               $minute=floor(($clean['start_time']-time())%86400/60);
               $obj['min'] = $minute;

               error_log("==2==");
            }else{
                $obj['min'] = "0";
                error_log("==3==");

            }
            if(count($cleans) >=1){
                $newList[] = $obj;

            }
        }
        $this->success('',$newList);
    }

    public function indexV2()
    {

        $user = $this->auth->getUser();
        $shop_id = $this->request->param('shop_id');
        $starTime = strtotime("-720 minute", time());
        $roomCleanModel = new Roomclean();
        $list = $roomCleanModel->with(['shop', 'room'])->where('Roomclean.shop_id', $shop_id)->where('Roomclean.status', 'in', ['done', 'doing', 'undo'])->where('end_time', ">=", $starTime)->order('start_time', 'asc')->group('room_id')->select();
        $dataList = $roomCleanModel->where('shop_id', $shop_id)->where('end_time', ">=", $starTime)->order('start_time', 'asc')->select();

        $newList = array();
        foreach ($list as $key => $val) {
            $obj = array();
            $obj['shop_name'] = $val['shop']['name'];
            $obj['room_name'] = $val['room']['name'];
            $obj['logo'] = cdnurl($val['room']['logo'], true);

            $cleanList = array();

            $lastKey = -1;
            $obj['isShow'] = -1;
            $min = 0;
            $id = -1;
            $isOneMore = 0;

            foreach ($dataList as $k => $v) {

                if ($val['room_id'] == $v['room_id']) {
                    $diff_seconds = $v['start_time'] - time();
                    if ($diff_seconds <= 0) {
                        $lastKey = $k;

                    } else {
                        $minutes = floor($diff_seconds / 60);
                        if (count($cleanList) == 0) {
                            $min = $minutes;
                        }


                        if ($lastKey != -1) {
                            $lastValue = $dataList[$lastKey];
                            $lastKey = -1;
                            if (in_array($lastValue['status'], ['doing', 'undo'])) {
                                $obj['isShow'] = 1;
                                $id = $lastValue['id'];
                                $min = $minutes;
                                $cleanList[] = $lastValue;
                                if($val['room_id'] == '3099'){

                                    error_log($id."=====dddd2=======".count($cleanList));
                                }
                            }


                        }
                        if (count($cleanList) <= 2) {
                            $cleanList[] = $v;
                            if (count($cleanList) == 2) break;
                        }
                    }
                }
            }

            if (count($cleanList) == 0 && $lastKey != -1) {
                $lastValue = $dataList[$lastKey];
                $lastKey = -1;
                if (in_array($lastValue['status'], ['doing', 'undo'])) {
                    $obj['isShow'] = 1;
                    $id = $lastValue['id'];
                    //$min = 0;
                    $cleanList[] = $lastValue;
                }
            }

            $obj['min'] = $min;
            $obj['id'] = $id;
            $obj['cleanList'] = $cleanList;
            if (count($cleanList) > 0) {
                $newList[] = $obj;
            }
        }

        $this->success('', $newList);
    }

    public function getShopList(){

        $user = $this->auth->getUser();
        $shopStaffModel = new Shopstaff();
        $list = $shopStaffModel->with(['shop'])->where('Shopstaff.mobile',$user['mobile'])->select();

        $newList = array();
        foreach ($list as $key=>$val){
            $newList[] = $val['shop'];
        }
        $this->success('',$newList);
    }

    public function cleanList(){

        $user = $this->auth->getUser();
        $shop_id = $this->request->param('shop_id');

        $roomCleanModel = new Roomclean();
        $all=$roomCleanModel->where('user_id',$user['id'])->where('shop_id',$shop_id)->count('id');
        $undo=$roomCleanModel->where('status','undo')->where('user_id',$user['id'])->where('shop_id',$shop_id)->count('id');
        $doing=$roomCleanModel->where('status','doing')->where('user_id',$user['id'])->where('shop_id',$shop_id)->count('id');
        $done=$roomCleanModel->where('status','done')->where('user_id',$user['id'])->where('shop_id',$shop_id)->count('id');
        $undone=$roomCleanModel->where('status','undone')->where('user_id',$user['id'])->where('shop_id',$shop_id)->count('id');


        $roomCleanModel = new Roomclean();
        $list = $roomCleanModel->with(['room'])->where('Roomclean.shop_id',$shop_id)->where('Roomclean.status','in',['undo','doing'])->select();
        foreach ($list as $key=>$val){
            $list[$key]['status_txt'] = $roomCleanModel->getStatus($val['status']);
            $list[$key]['room']['logo'] = cdnurl($val['room']['logo'],true);
            if(time() < $val['start_time']){
                $minute=floor(($val['start_time']-time())%86400/60);
                $list[$key]['min'] = $minute;
            }else{
                $list[$key]['min'] = "";
            }
        }

        $ret = array();
        $ret['all'] = $all;
        $ret['done'] = $done;
        $ret['undone'] = $undone;
        $ret['undo'] = $undo;
        $ret['list'] = $list;
        $this->success('',$ret);

    }



    


    public function done(){

        $this->success('');
    }


}
